<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Middleware;

use App\Common\Tool;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class CheckLogin
{

    public function handle(Request $request, Closure $next)
    {
        if (empty($request->get('uid')) || empty($request->get('skey'))) {
            return response(Tool::jsonHandle([],301, 'uid or skey must needed'));
        }

        if (config::get('app.debug')) {
            $skey = DB::table('t_player_info')->where('id', $request->get('uid'))->value('skey');
            if (!$skey) {
                return response(Tool::jsonHandle([],505,"get skey error.\n"));
            }
            if ($skey != $request->get('skey')) {
                return response(Tool::jsonHandle([],506, "skey error"));
            }
        }
        return $next($request);
    }

}